<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Api.php
 * Created on : 2021-3-1 14:52:05
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace miniapp\fleet\controller;

use miniapp\fleet\model\WechatAppAddonFleetGroup;
use haoteam\service\UtilService;

/**
 * 
 * Class Api
 * Tiger <1192851302@qq.com> 2021-3-1 14:52:05
 */
class Api {

    /**
     * 申请群组
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function GroupApply() {
        $post = UtilService::postMore([
                    'name'
        ]);
        if (empty($post['name'])) {
            return app('json')->fail('名称不能为空');
        }
        $group_apply_number = miniapp_addon_config('group_apply_number');
        if ($group_apply_number <= WechatAppAddonFleetGroup::be(['mid' => request()->mid(), 'uid' => request()->uid()])) {
            return app('json')->fail('你最多只能申请' . $group_apply_number . '个群组');
        }
        //匹配相似度
        try {
            WechatAppAddonFleetGroup::create(['mid' => request()->mid(), 'uid' => request()->uid(), 'name' => $post['name']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('申请成功，等待审核');
    }

    /**
     * 申请群组
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function GroupUpdate() {
        $post = UtilService::postMore([
                    'id',
                    'name'
        ]);
        if (empty($post['name'])) {
            return app('json')->fail('名称不能为空');
        }
        if (empty($post['id'])) {
            return app('json')->fail('ID不能为空');
        }
        //匹配相似度
        try {
            WechatAppAddonFleetGroup::update([
                'name' => $post['name'],
                'status' => 0
                    ], [
                'id' => $post['id'],
                'uid' => request()->uid()
            ]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('更新成功，等待审核');
    }

}
